<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 角色列表和添加角色 -->
    <el-card  row-Key="id">
      <el-button type="primary" @click="addRolesDiaVis = true" id="addRoles">添加角色</el-button>
      <el-table :data="rolesList" style="width: 100%" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
              <!-- 循环一级权限,所有的底边框加border,第一个顶部加border -->
            <el-row class="bd_bottom" :class='i==0?"bd_top":""' v-for="(item,i) in scope.row.children" :key="item.id">
              <!-- 展示一级权限的文字 -->
              <el-col :span='6'>
                <el-tag closable>{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 展示二级数据 -->
              <el-col :span='18'>
                  <!-- 循环二级权限 ,所有的顶部加border,第一个顶部不加border-->
                  <el-row :class='i==0?"":"bd_top"' v-for="(item2,i) in item.children" :key='item2.id'>
                      <el-col :span='6'>
                          <el-tag closable type='success'>{{item2.authName}}</el-tag>
                          <i class="el-icon-caret-right"></i>
                      </el-col>
                      <!-- 循环三级权限 -->
                      <el-col :span='18'>
                         <el-tag @close='removeTag(scope.row,item3.id)' closable v-for="item3 in item2.children" :key='item3.id' type='warning'>
                            {{item3.authName}}
                         </el-tag>
                      </el-col>
                  </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-button @click="showSetRightDialog(scope.row)" type="warning" size="mini" icon="el-icon-setting">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加角色对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="addRolesDiaVis"
      width="50%">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        
      </span>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog 
        @close='resetDefKeys'
        :visible.sync="setRightDialogVisible"
        title="权限分配"
        width="50%">
        <el-tree style='height:350px; overflow:scroll;' ref='refTree' :data="rightsList" :props="treeProps" :default-checked-keys='defkeys' show-checkbox default-expand-all node-key="id"></el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRightDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="setRightsSub">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],

      // 添加角色对话框隐藏与显示
      addRolesDiaVis:false,
      // 控制分配权限对话框的显示与隐藏
      setRightDialogVisible:false,
      // 用于保存权限信息
      rightsList:[],
      // 属性控件
      treeProps:{
        children: 'children',
        label: 'authName'
      },
      // 保存默认选中的权限,保存的是每个权限的id
      defkeys:[],
      // 保存当前角色Id
      roleId:0
    };
  },

  methods: {
    async getRolesList() {
      const res = await this.axios.get("roles");
      if (res.meta.status == 200) {
        this.rolesList = res.data;
        // console.log(this.rolesList);
      }
    },

    // 删除三级权限标签
    removeTag(row,rightId){
      this.$cfm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // console.log('确认删除');
          const res=await this.axios.delete(`roles/${row.id}/rights/${rightId}`);
          console.log(res);
          if(res.meta.status==200){
            this.$mg.success('删除成功!');
            row.children=res.data;
          }
        }).catch(() => {
          this.$mg.info('已取消删除');        
        });
    },
    // 分配权限对话框
    async showSetRightDialog(row){
      this.roleId=row.id;
      this.setRightDialogVisible=true;
      const res=await this.axios.get('rights/tree');
      // 返回数据标识成功
      if(res.meta.status==200){
        this.rightsList=res.data;
        // console.log(this.rightsList); 
      }

      // 获取当前行的数据
        this.getAllKeys(row)
    },
    // 通过递归调用三级权限id
    getAllKeys(row){
      row.children.forEach(item => {
        // 没有children字段表示已经是三级权限了
        if(!item.children){
          return this.defkeys.push(item.id);
        }
        this.getAllKeys(item);
      });
    },
    // 清空原来的数组
    resetDefKeys(){
      this.defkeys=[];
    },

    // 设置权限对话框确定按钮
    async setRightsSub(){
      console.log(this.roleId);

      var keysAll=[
        ...this.$refs.refTree.getCheckedKeys(),
        ...this.$refs.refTree.getHalfCheckedKeys()
        ]
      var idStr=keysAll.join(',');
      console.log(idStr);
      const res=await this.axios.post(`roles/${this.roleId}/rights`,{
        rids:idStr
      })
      console.log(res);
      if(res.meta.status==200){
        this.$mg.success(res.meta.msg);
        this.setRightDialogVisible=false;
        this.getRolesList();
      }
    }

    // 添加jues


  },

  created() {
    this.getRolesList();
  }
};
</script>

<style lang='less' scoped>
.el-tag {
  margin: 8px;
}
.bd_bottom {
  border-bottom: 1px solid #eee;
}
.bd_top {
  border-top: 1px solid #eee;
}
.el-row{
    display: flex;
    align-items: center;
}
</style>
